package com.sibu.yx.promotion.admin.web.controller.common;

import com.doubo.common.model.response.CommonResponse;
import com.doubo.json.util.JsonUtil;
import com.sibu.yx.promotion.admin.web.util.HttpUtil;
import com.sibu.yx.promotion.common.enums.ErrorCodeEnum;
import com.sibu.yx.promotion.common.manage.ResponseManage;
import com.sibu.yx.promotion.persistence.config.AppConfig;

import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: qiujingwang
 * Date: 2016-4-9
 * Depiction:公用页面请求
 */
@Slf4j
@RestController
public class CommonController {


    @RequestMapping(value = "/")
    public CommonResponse index() {
        return ResponseManage.success(AppConfig.getInstance().getEnvironmentName());
    }

    @RequestMapping(value = "/error/403")
    @ResponseBody
    public void error403(HttpServletRequest request,
                         HttpServletResponse response, Exception ex) {
        HttpUtil.writeJsonDate(response, JsonUtil.toJson(ResponseManage.fail(ErrorCodeEnum.ERR_403_ERROR)));
    }

    @RequestMapping(value = "/error/404")
    @ResponseBody
    public void error404(HttpServletRequest request,
                         HttpServletResponse response) {
        HttpUtil.writeJsonDate(response, JsonUtil.toJson(ResponseManage.fail(ErrorCodeEnum.ERR_404_ERROR)));
    }

    @RequestMapping(value = "/error/500")
    @ResponseBody
    public void error500(HttpServletRequest request,
                         HttpServletResponse response, Throwable ex) {
        HttpUtil.writeJsonDate(response, JsonUtil.toJson(ResponseManage.fail(ErrorCodeEnum.ERR_500_ERROR)));

    }
}
